package br.com.mmm.javacenter.framework.view.validators;
import java.io.*;
import java.net.URISyntaxException;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import org.xml.sax.SAXException;

public class XHTMLValidator {
	public static void main(String[] args) throws SAXException, IOException, URISyntaxException {

		// 1. Lookup a factory for the W3C XML Schema language
		SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

		// 2. Compile the schema.
		// Here the schema is loaded from a java.io.File, but you could use
		// a java.net.URL or a javax.xml.transform.Source instead.
		;
		File schemaLocation = new File(Thread.currentThread().getContextClassLoader().getResource("xhtml1-strict.dtd").toURI());
		
		Schema schema = factory.newSchema(schemaLocation);

		// 3. Get a validator from the schema.
		Validator validator = schema.newValidator();

		// 4. Parse the document you want to check.
		Source source = new StreamSource("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\"\r\n" + 
				"      xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + 
				"      xsi:schemaLocation=\"http://www.w3.org/1999/xhtml\r\n" + 
				"                          http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd\"><a>teste</a></html>");

		// 5. Check the document
		try {
			validator.validate(source);
			System.out.println(args[0] + " is valid.");
		} catch (SAXException ex) {
			System.out.println(args[0] + " is not valid because ");
			System.out.println(ex.getMessage());
		}

	}

}
